/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.secure.web.group;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.secure.controll.SecureGroupFacadeLocal;
import org.fyx.secure.controll.SecureRoleFacadeLocal;
import org.fyx.secure.ejb.SecureRoles;
import org.fyx.secure.entity.SecureGroup;
import org.fyx.secure.entity.SecureRole;
import org.fyx.util.FyxUtility;

/**
 *
 * @author xljan
 */
@ManagedBean
@SessionScoped
public class SecureGroupAdd {
    private  SecureGroup group = null;
    private SecureRole[] selectedRoles;

    @EJB
    private SecureGroupFacadeLocal secureGroupFacade;
    @EJB
    private SecureRoleFacadeLocal secureRoleFacade;
    @EJB
    private MulaLocal mula;
    @EJB
    private LinkerLocal linker;
    
    @PostConstruct
    public void postConstruct(){
        FyxUtility.checkRole(SecureRoles.GROUP_MANAGER);
    }
    
    public SecureGroup getGroup() {
        return group;
    }

    public void setGroup(SecureGroup group) {
        this.group = group;
    }

    public SecureRole[] getSelectedRoles() {
        return selectedRoles;
    }

    public void setSelectedRoles(SecureRole[] selectedRoles) {
        this.selectedRoles = selectedRoles;
    }
    
    /** Creates a new instance of SecureGroupAdd */
    public SecureGroupAdd() {
        reset();
    }
    
    private void reset(){
        group = new SecureGroup(0);
        group.setSecureRoleCollection(new LinkedList<SecureRole>());
        selectedRoles = null;
    }
    
    public void create(){
        if(secureGroupFacade.find(group.getIdGroup()) != null){
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, 
                    mula.get(mula.get("The name of group alredy exist.")), null);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return;
        }
        try{
            group.getSecureRoleCollection().addAll(Arrays.asList(selectedRoles));
            secureGroupFacade.create(group);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                    mula.get(mula.get("Group was created sucessfull.")), null);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            FacesContext.getCurrentInstance().getExternalContext().
                    redirect(linker.getLink("secure/group/list.xhtml"));
        }catch(Throwable e){
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                    mula.get("Operation fail."), mula.get("Name must be unique"));
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        reset();
    }
    
    public List<SecureRole> getAllRoles(){
        return secureRoleFacade.findAll();
    }
    
}
